﻿using System;
using System.Net;
using Lextm.SharpSnmpLib;

namespace SManage.TraceEngine
{
    [Serializable]
    public sealed class SyncItem:IEquatable<SyncItem>,IComparable<SyncItem>
    {
        public SyncItem(IPEndPoint ipEndPoint) : this(ipEndPoint.Address, VersionCode.V2, ipEndPoint.Port) { }

        public SyncItem(IPAddress targetIP, VersionCode version, Int32 port = 161, String community = "public", TimeSpan interval = default(TimeSpan)) {
            EndPoint = new IPEndPoint(targetIP, port);
            Community = new OctetString(community);
            Version = version;
            if (interval == default(TimeSpan)) {
                Interval = TimeSpan.FromHours(5);
            } else {
                Interval = interval;
            }
            LastSyncTime = DateTime.Now;
        }

        public IPEndPoint EndPoint {
            get;
            private set;
        }
        public OctetString Community {
            get;
            private set;
        }
        public VersionCode Version {
            get;
            private set;
        }
        public TimeSpan Interval {
            get;
            private set;
        }
        public Int32 SyncCount {
            get;
            private set;
        }
        public DateTime LastSyncTime {
            get;
            private set;
        }
        public String Description { get; set; }

        public void Update() {
            LastSyncTime = DateTime.Now;
            SyncCount++;
        }

        public bool Equals(SyncItem other) {
            var equal = EndPoint == other.EndPoint
                && Interval == other.Interval;
            return equal;
        }

        public int CompareTo(SyncItem other) {
            return EndPoint.Equals(other.EndPoint)?0:-1;
        }
    }
}
